window.Note = Backbone.Model.extend({
    
    defaults: {
    	
        date: Date.today(),
        content: "",
        imageContent: "",
        category: "category-blue"
    },

    toJSON: function() {
        // 调用super.toJSON()
        var json = Note.__super__.toJSON.apply(this, arguments);
        // 追加dateInfo
        _.extend(json, {dateInfo: this.dateInfo()});
        return json;
    },

    // 获得日期的字符串表示形式
    dateInfo: function() {
        return this.get("date").toString("yyyy-M-d");
    }

});

window.Note.fromJSONLikeData = function(data) {
    return new Note({
        id: data.id, 
        content: data.content, 
        date: Date.parse(data.dateInfo == null ? data.date : data.dateInfo), 
        imageContent: data.imageContent,
        category: data.category
    });
};

window.NoteList = Backbone.Collection.extend({

    model: Note,

    toJSONString: function() {
        return JSON.stringify(this);
    },

    loadFromJSONString: function(data) {
        data = data.replace(/\\("|'|\\)/g, "$1");
        var json = JSON.parse(data);
        for (var i = 0; i < json.length; i++) {
            var note = Note.fromJSONLikeData(json[i]);
            this.add(note);
        }
    }
    
});